package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.PinyinUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.util.AppUtil;
import com.ccp.dev.extend.service.ExtendSysOrgService;
import com.ccp.dev.form.service.impl.ScriptImpl;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdFalProblemresetDao;
import com.ccp.dev.qbdms.dao.WQbdFlightTestDao;
import com.ccp.dev.qbdms.dao.WQbdReportTemplateDao;
import com.ccp.dev.qbdms.model.WQbdReportTemplate;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.BaseUtil.SystemLevel;
import com.ccp.dev.qbdms.util.ProcessingTimeUtil;
import com.ccp.dev.system.dao.DictionaryDao;
import com.ccp.dev.system.dao.GlobalTypeDao;
import com.ccp.dev.system.model.Dictionary;
import com.ccp.dev.system.model.GlobalType;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.service.DictionaryService;
import com.ccp.dev.system.service.SysOrgService;
import com.google.common.collect.Maps;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.apache.commons.compress.utils.Lists;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 报告模板管理 信息操作服务类
 *
 * @author milihao
 * @date 2020-06-22 15:26:43
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdReportTemplateService extends BaseService<WQbdReportTemplate> {

	@Resource
	public WQbdReportTemplateDao dao;
	@Resource
	public WQbdFalProblemresetDao wQbdFalProblemresetDao;

	@Resource
	public GlobalTypeDao globalTypeDao;
	@Resource
	public DictionaryDao dictionaryDao;
	@Resource
	public WQbdFlightTestDao wQbdFlightTestDao;

	@Resource
	public SysOrgService sysOrgService;
	@Resource
	public ExtendSysOrgService extendSysOrgService;
	@Resource
	public ScriptImpl scriptimpl;
	@Resource
	private DictionaryService dictionaryService;
	/**
	 * 柱图类型
	 */
	public final int CHART_BAR = 1;
	/**
	 * 饼图类型
	 */
	public final int CHART_PIE = 2;
	/**
	 * 表格
	 */
	public final int TABLE = 3;

	/**
	 * 研制
	 */
	public final int DEVELOPMENT = 1;
	/**
	 * 批产
	 */
	public final int MASS_PRODUCTION = 2;

	public HSSFWorkbook exportExcel(Excel excel, QueryFilter queryFilter) {
		List<WQbdReportTemplate> dataList = this.dao.queryAll(queryFilter);
		int startRow = 1;
		//给每一个单元格赋值
		for (WQbdReportTemplate data : dataList) {
			excel.cell(startRow, 0).value(data.getId());
			//excel.cell(startRow, 1).value(data.getMc());
			//可在赋值时，根据需求进行格式化
			//excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
			//excel.cell(startRow, 3).value(data.getSj());
			//excel.cell(startRow, 4).value(data.getTenantId());
			startRow++;
		}
		return excel.getWorkBook();
	}


	public List<Map<String, Object>> getCharData(WQbdReportTemplate wQbdReportTemplate,
			QueryFilter queryFilter) {
		List<Map<String, Object>> list = new ArrayList<>();
		// 添加密级过滤
		ZhiLiangConst.addQueryFilter(queryFilter);
		Map<String, Object> querymap = new HashMap<>();
		querymap.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
		querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
		querymap.put("reportStatus", queryFilter.get("reportStatus"));
		//质量问题统计
		list.add(getQualityProblemData(CHART_BAR, wQbdReportTemplate, querymap));
		list.add(getQualityProblemData(TABLE, wQbdReportTemplate, querymap));
		//研制型号按原因统计
		list.add(
				getProblemChartDateByWorkPhase(CHART_BAR, wQbdReportTemplate, DEVELOPMENT, querymap));
		list.add(
				getProblemChartDateByWorkPhase(CHART_PIE, wQbdReportTemplate, DEVELOPMENT, querymap));
		//批产型号按原因统计
		list.add(getProblemChartDateByWorkPhase(CHART_BAR, wQbdReportTemplate, MASS_PRODUCTION,
				querymap));
		list.add(getProblemChartDateByWorkPhase(CHART_PIE, wQbdReportTemplate, MASS_PRODUCTION,
				querymap));
		//研制型号按阶段统计
		list.add(getModelDevChartDateByWorkPhase(CHART_BAR, wQbdReportTemplate, querymap));
		list.add(getModelDevChartDateByWorkPhase(CHART_PIE, wQbdReportTemplate, querymap));
		//批产型号按阶段统计
		list.add(getProduceChartDataByWorkPhase(CHART_BAR, wQbdReportTemplate, querymap));
		list.add(getProduceChartDataByWorkPhase(CHART_PIE, wQbdReportTemplate, querymap));
		// 前五位汇总表
		list.add(getTop5(wQbdReportTemplate, querymap));
		//靶场零故障中汇总表
		list.add(getZeroFailure(wQbdReportTemplate, querymap));
		// 售后质量问题
		list.add(getAfterSaleQualityBarChartData(wQbdReportTemplate, querymap));
		list.add(getAfterSaleQualityPieChartData(wQbdReportTemplate, querymap));
		//军贸质量问题统计
		list.add(getProblemResetHistogramDataByModelType(CHART_BAR, wQbdReportTemplate, querymap));
		list.add(getProblemResetHistogramDataByModelType(CHART_PIE, wQbdReportTemplate, querymap));

		return list;
	}

	public Map<String, Object> getZeroFailure(WQbdReportTemplate wQbdReportTemplate,
			Map<String, Object> paramMap) {
		paramMap.put("startTime", wQbdReportTemplate.getZeroFailShootingRangeStart());
		paramMap.put("endTime", wQbdReportTemplate.getZeroFailShootingRangeEnd());
		List<Map<String, Object>> zeroFailureMapList;
		if (BaseUtil.getCurrentSystemLevel().equals(SystemLevel.GROUPLEVEL)) {
			zeroFailureMapList = wQbdFlightTestDao.getZeroFailureByGroup(paramMap);
		} else {
			zeroFailureMapList = wQbdFlightTestDao.getZeroFailureByHospital(paramMap);
		}

		for (Map zeroFailure : zeroFailureMapList) {
			if (null == zeroFailure.get("COUNTA")) {
				zeroFailure.put("COUNTA", 0);
			}
			if (null == zeroFailure.get("COUNTB")) {
				zeroFailure.put("COUNTB", 0);
			}
			zeroFailure.put("COUNTB",
					Integer.parseInt(String.valueOf(zeroFailure.get("COUNTA"))) - Integer.parseInt(
							String.valueOf(zeroFailure.get("COUNTB"))));
			if (null == zeroFailure.get("COUNTC")) {
				zeroFailure.put("COUNTC", 0);
			}
			if (null == zeroFailure.get("COUNTD")) {
				zeroFailure.put("COUNTD", 0);
			}
			if (null == zeroFailure.get("COUNTE")) {
				zeroFailure.put("COUNTE", 0);
			}
			if (null == zeroFailure.get("COUNTF")) {
				zeroFailure.put("COUNTF", 0);
			}
		}
		Map returnDataMap = Maps.newHashMap();
		returnDataMap.put("data", zeroFailureMapList);
		return returnDataMap;

	}

	/**
	 * 研制型号按原因统计
	 *
	 * @param chartType 类型
	 * @param wQbdReportTemplate 实体
	 * @return map
	 */
	public Map<String, Object> getProblemChartDateByWorkPhase(int chartType,
			WQbdReportTemplate wQbdReportTemplate, int workPhaseType, Map<String, Object> paramMap) {
		paramMap.putAll(getParamMap(wQbdReportTemplate));
		String title = "";
		if (workPhaseType == DEVELOPMENT) {
			//指定型号研制阶段
			paramMap.put("modeldev", "'模样', '初样', '试样', '定型'");
			title = "研制";
		}
		if (workPhaseType == MASS_PRODUCTION) {
			//指定型号批产阶段
			paramMap.put("modeldev", "'批产'");
			title = "批产";
		}
		Map<String, Object> returnMap = Maps.newHashMap();
		List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
				.getProblemChartDateByWorkPhase(paramMap);
		List legendData = Lists.newArrayList();
		List seriesData = Lists.newArrayList();
		List<Map> pieDataList = Lists.newArrayList();
		int secretValue = 0;
		String secret = "";
		for (Map dataMap : dataMapList) {
			if (null != dataMap.get("CAUSATIONCLASS1")) {
				seriesData.add(dataMap.get("CAUSATIONCLASS1"));
				legendData.add(Integer.parseInt(String.valueOf(dataMap.get("COUNT"))));
				if (chartType == CHART_PIE) {
					Map pieMap = Maps.newHashMap();
					pieMap.put("name", dataMap.get("CAUSATIONCLASS1"));
					pieMap.put("value", Integer.parseInt(String.valueOf(dataMap.get("COUNT"))));
					pieDataList.add(pieMap);
				}
			}
			secretValue = getSecretValue(dataMap, secretValue);
		}
		if (0 != secretValue) {
			secret = getSecret(String.valueOf(secretValue));
		}
		if (chartType == CHART_BAR) {
			returnMap.put("datalist", legendData);
			returnMap.put("title",
					paramMap.get("title").toString() + "年" + title + "型号质量问题按原因柱状图" + secret);
		} else {
			returnMap.put("title",
					paramMap.get("title").toString() + "年" + title + "型号质量问题按原因统计" + secret);
			returnMap.put("datalist", pieDataList);
		}

		returnMap.put("xtitlelist", seriesData);
		return returnMap;
	}

	public Map getParamMap(WQbdReportTemplate wQbdReportTemplate) {
		Map<String, Object> paramMap = Maps.newHashMap();
		List<Map<String, Object>> timeMap = ProcessingTimeUtil
				.packagingTime(wQbdReportTemplate.getQualityProblem(),
						wQbdReportTemplate.getQualityProblemStart(),
						wQbdReportTemplate.getQualityProblemEnd());
		String title = "";
		if (timeMap.size() > 0) {
			title = timeMap.get(0).get("title").toString() + "至" + timeMap.get(timeMap.size() - 1)
					.get("title").toString();
		}
		paramMap.put("startTime", timeMap.get(0).get("startTime"));
		paramMap.put("endTime", timeMap.get(timeMap.size() - 1).get("endTime"));
		paramMap.put("title", title);
		paramMap.put("timeMap", timeMap);
		return paramMap;
	}

	/**
	 * 质量问题原因统计图
	 *
	 * @param wQbdReportTemplate 参数实体
	 * @param chartType 类型
	 * @return Map
	 */
	public Map<String, Object> getQualityProblemData(int chartType,
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> paramMap) {
		List<Map<String, Object>> timeMapList = ProcessingTimeUtil
				.packagingTime(wQbdReportTemplate.getQualityProblem(),
						wQbdReportTemplate.getQualityProblemStart(),
						wQbdReportTemplate.getQualityProblemEnd());
		Map<String, Object> returnMap = Maps.newHashMap();

		List<Map<String, Object>> returnMapList = Lists.newArrayList();

		paramMap.put("startTime", timeMapList.get(0).get("startTime"));
		paramMap.put("endTime", timeMapList.get(timeMapList.size() - 1).get("endTime"));
		List<Map<String, Object>> allDataMapList = wQbdFalProblemresetDao
				.getQualityProblemData(paramMap);
		String secret = "";
		if (chartType == TABLE) {
			return getTableData(returnMap, timeMapList, paramMap);
		}
		int secretValue = 0;
		List<String> xAxisData = Lists.newArrayList();
		for (Map dataMap : allDataMapList) {
			if (null != dataMap.get("CAUSATIONCLASS1")) {
				String name = String.valueOf(dataMap.get("CAUSATIONCLASS1"));
				if (!xAxisData.contains(name)) {
					xAxisData.add(name);
				}
			}
			secretValue = getSecretValue(dataMap, secretValue);
		}
		if (0 != secretValue) {
			secret = getSecret(String.valueOf(secretValue));
		}
		List<Object> legendList = Lists.newArrayList();
		List<String> lineLegendList = Lists.newArrayList();
		int j = 0;
		for (Map timeMap : timeMapList) {
			paramMap.put("startTime", timeMap.get("startTime"));
			paramMap.put("endTime", timeMap.get("endTime"));
			List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
					.getQualityProblemData(paramMap);
			Map picDataMap = Maps.newHashMap();
			Map percentDataMap = Maps.newHashMap();
			picDataMap.put("name", timeMap.get("title").toString());
			Map mapExample = Maps.newHashMap();
			mapExample.put("name", timeMap.get("title").toString());
			mapExample.put("icon", "circle");
			legendList.add(mapExample);
			picDataMap.put("type", "bar");
			percentDataMap.put("name", timeMap.get("title").toString() + "占比");
			percentDataMap.put("type", "line");
			percentDataMap.put("yAxisIndex", 1);
			percentDataMap.put("smooth", true);
			if (j == 0) {
				percentDataMap.put("symbol", "circle");
			} else if (j == 1) {
				percentDataMap.put("symbol", "diamond");
			} else {
				percentDataMap.put("symbol", "rectangle");
			}

			percentDataMap.put("symbolSize", 8);
			lineLegendList.add(timeMap.get("title").toString() + "占比");
			List numberList = Lists.newArrayList();
			List percentList = Lists.newArrayList();
			for (int i = 0; i < xAxisData.size(); i++) {
				numberList.add(0);
				percentList.add(0);
			}
			int count = 0;
			for (Map dataMapCount : dataMapList) {
				count += Integer.parseInt(String.valueOf(dataMapCount.get("COUNT")));
			}
			for (Map dataMap : dataMapList) {
				if (null != dataMap.get("CAUSATIONCLASS1")) {
					if (xAxisData.indexOf(dataMap.get("CAUSATIONCLASS1")) != -1) {
						numberList.set(xAxisData.indexOf(dataMap.get("CAUSATIONCLASS1")),
								dataMap.get("COUNT"));
						Map map1 = Maps.newHashMap();
						percentList.set(xAxisData.indexOf(dataMap.get("CAUSATIONCLASS1")),
								Double.parseDouble(new DecimalFormat("0").format(
										Double.parseDouble(String.valueOf(dataMap.get("COUNT"))) / count
												* 100)));
					}
				}

			}
			picDataMap.put("data", numberList);
			percentDataMap.put("data", percentList);
			returnMapList.add(picDataMap);
			returnMapList.add(percentDataMap);
			j++;
		}
		returnMap.put("data", returnMapList);
		returnMap.put("xAxis", xAxisData);
		for (String s : lineLegendList) {
			legendList.add(s);
		}
		returnMap.put("legend", legendList);
		returnMap.put("title", "型号质量问题统计" + secret);
		return returnMap;
	}


	/**
	 * 质量问题原因汇总表格
	 *
	 * @param returnMap 返回map
	 * @param timeMapList 时间map
	 * @param paramMap 参数map
	 * @return map
	 */
	public Map getTableData(Map returnMap, List<Map<String, Object>> timeMapList, Map paramMap) {
		GlobalType globalType = globalTypeDao.getByDictNodeKey("yyfls");
		List<Dictionary> dictionaryList = dictionaryDao.getDicByParentId(globalType.getTypeId());
		List<String> diNameList = Lists.newArrayList();
		for (Dictionary dictionary : dictionaryList) {
			diNameList.add(dictionary.getItemName());
		}
		List<Map> numberMap = Lists.newArrayList();
		List<Map> percentMap = Lists.newArrayList();
		for (Map timeMap : timeMapList) {
			paramMap.put("startTime", timeMap.get("startTime"));
			paramMap.put("endTime", timeMap.get("endTime"));
			List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
					.getQualityProblemData(paramMap);
			int count = 0;
			for (Map mapCount : dataMapList) {
				count += Integer.parseInt(String.valueOf(mapCount.get("COUNT")));
			}
			Map numberDataMap = Maps.newHashMap();
			numberDataMap.put("title", "数量");
			numberDataMap.put("year", timeMap.get("title"));
			Map percentDataMap = Maps.newHashMap();
			percentDataMap.put("title", "百分比");
			percentDataMap.put("year", timeMap.get("title"));
			for (Map map11 : dataMapList) {
				if (null != map11.get("CAUSATIONCLASS1")) {
					if (diNameList.contains(map11.get("CAUSATIONCLASS1"))) {
						numberDataMap.put(PinyinUtil
										.getPinYinHeadChar(map11.get("CAUSATIONCLASS1").toString()),
								map11.get("COUNT"));
						percentDataMap.put(PinyinUtil
										.getPinYinHeadChar(map11.get("CAUSATIONCLASS1").toString()),
								Double.parseDouble(new DecimalFormat("0").format(
										Double.parseDouble(String.valueOf(map11.get("COUNT"))) / count
												* 100)) + "%");
					}
				}
			}
			numberMap.add(numberDataMap);
			percentMap.add(percentDataMap);
		}
		for (Map map : percentMap) {
			numberMap.add(map);
		}
		returnMap.put("data", numberMap);
		return returnMap;
	}

	/**
	 * 军贸质量问题统计柱状图数据
	 *
	 * @param wQbdReportTemplate 实体
	 * @return map
	 */
	public Map<String, Object> getProblemResetHistogramDataByModelType(
			int chartType, WQbdReportTemplate wQbdReportTemplate, Map<String, Object> paramMap) {
		List<Map<String, Object>> timeMapList = ProcessingTimeUtil
				.packagingTime(wQbdReportTemplate.getMilitaryTradeQuality(),
						wQbdReportTemplate.getMilitaryTradeQualityStart(),
						wQbdReportTemplate.getMilitaryTradeQualityEnd());
		Map<String, Object> returnMap = Maps.newHashMap();
		String title = "";
		if (timeMapList.size() > 1) {
			title = timeMapList.get(0).get("title").toString().substring(0, 4) + "至" + timeMapList
					.get(timeMapList.size() - 1)
					.get("title").toString().substring(0, 4);
		} else {
			title = timeMapList.get(0).get("title").toString();
		}
		int secretValue = 0;
		String secret = "";
		if (chartType == CHART_BAR) {
			List<String> xData = Lists.newArrayList();
			List<Integer> yData = Lists.newArrayList();
			for (Map timeMap : timeMapList) {
				paramMap.put("startTime", timeMap.get("startTime"));
				paramMap.put("endTime", timeMap.get("endTime"));
				List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
						.getProblemResetHistogramDataByModelType(paramMap);
				xData.add(timeMap.get("title").toString());
				if (dataMapList.size() > 0) {
					for (Map countMap : dataMapList) {
						secretValue = getSecretValue(countMap, secretValue);
						if (null != countMap.get("MODELTYPE") && countMap.get("MODELTYPE")
								.equals("军贸产品")) {
							yData.add(Integer.parseInt(String.valueOf(countMap.get("COUNT"))));
							break;
						} else {
							yData.add(0);
						}
					}
				} else {
					yData.add(0);
				}
			}
			returnMap.put("xtitlelist", xData);
			returnMap.put("datalist", yData);
		} else {
			int count = 0;
			int militaryTradeCount = 0;
			List<Map> legendData = Lists.newArrayList();
			List seriesData = Arrays.asList("军贸质量问题", "其他质量问题");
			for (Map timeMap : timeMapList) {
				paramMap.put("startTime", timeMap.get("startTime"));
				paramMap.put("endTime", timeMap.get("endTime"));
				List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
						.getProblemResetHistogramDataByModelType(paramMap);
				for (Map countMap : dataMapList) {
					int number = Integer.parseInt(String.valueOf(countMap.get("COUNT")));
					count += number;
					secretValue = getSecretValue(countMap, secretValue);
					if (null != countMap.get("MODELTYPE") && countMap.get("MODELTYPE")
							.equals("军贸产品")) {
						militaryTradeCount += number;

					}
				}
			}
			Map<String, Object> histogramDataMap = Maps.newHashMap();
			histogramDataMap.put("name", "军贸质量问题");
			histogramDataMap.put("value", militaryTradeCount);
			legendData.add(histogramDataMap);
			Map<String, Object> otherMap = Maps.newHashMap();
			otherMap.put("name", "其他质量问题");
			otherMap.put("value", count - militaryTradeCount);
			legendData.add(otherMap);
			returnMap.put("datalist", legendData);
			returnMap.put("xtitlelist", seriesData);
		}
		secret = getSecret(String.valueOf(secretValue));
		if (chartType == CHART_BAR) {
			returnMap.put("title", title + "年军贸质量问题条形图" + secret);
		} else {
			returnMap.put("title", title + "军贸质量问题饼图" + secret);
		}

		return returnMap;
	}

	/**
	 * 获取售后质量问题柱状图数据
	 *
	 * @auth zzy
	 */
	public Map<String, Object> getAfterSaleQualityBarChartData(
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> querym) {
		List<Map<String, Object>> querydate = ProcessingTimeUtil
				.packagingTime(wQbdReportTemplate.getAfterSalesQuality(),
						wQbdReportTemplate.getAfterSalesQualityStart(),
						wQbdReportTemplate.getAfterSalesQualityEnd());
		String titlestr = ProcessingTimeUtil
				.converDateToTitleStr(wQbdReportTemplate.getAfterSalesQuality(),
						wQbdReportTemplate.getAfterSalesQualityStart(),
						wQbdReportTemplate.getAfterSalesQualityEnd());
		Map<String, Object> resultmap = new LinkedHashMap<>();
		List<String> titlelist = new ArrayList<>();
		List<Integer> datalist = new ArrayList<>();
		int secretValue = 0;
		String secret = "";
		for (Map<String, Object> querymap : querydate) {
			querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
			querymap.put("secret", scriptimpl.getCurrentUser().getPrivacyLevel());
			Map<String, Object> temp = wQbdFalProblemresetDao
					.getAfterSaleQualityBarDataByDate(querymap);
			int nums = 0;
			if (null != temp && null != temp.get("NUMS")) {
				nums = Integer.valueOf(temp.get("NUMS").toString());
			}
			secretValue = getSecretValue(temp, secretValue);
			secret = getSecret(String.valueOf(secretValue));
			datalist.add(nums);
			titlelist.add(querymap.get("title").toString());
		}
		resultmap.put("xtitlelist", titlelist);
		resultmap.put("datalist", datalist);
		resultmap.put("title", titlestr + "售后质量问题条形图" + secret);
		return resultmap;
	}

	/**
	 * 获取售后质量问题饼图数据
	 *
	 * @auth zzy
	 */
	public Map<String, Object> getAfterSaleQualityPieChartData(
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> querymap) {
		querymap.putAll(ProcessingTimeUtil
				.converSimpleDateRange(wQbdReportTemplate.getAfterSalesQuality(),
						wQbdReportTemplate.getAfterSalesQualityStart(),
						wQbdReportTemplate.getAfterSalesQualityEnd()));
		String titlestr = ProcessingTimeUtil
				.converDateToTitleStr(wQbdReportTemplate.getAfterSalesQuality(),
						wQbdReportTemplate.getAfterSalesQualityStart(),
						wQbdReportTemplate.getAfterSalesQualityEnd());
		Map<String, Object> resultmap = new LinkedHashMap<>();
		querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
		// 饼图中的时间需要处理
		List<Map<String, Object>> datalist = wQbdFalProblemresetDao
				.getAfterSaleQualityPieDataByDate(querymap);
		if (null == datalist || datalist.size() < 1) {
			return resultmap;
		}
		int secretValue = 0;
		String secret = "";
		List<String> titlelist = new ArrayList<>();
		for (Map<String, Object> stringObjectMap : datalist) {
			titlelist.add(String.valueOf(stringObjectMap.get("name")));
			secretValue = getSecretValue(stringObjectMap, secretValue);
		}
		secret = getSecret(String.valueOf(secretValue));
		resultmap.put("xtitlelist", titlelist);
		resultmap.put("datalist", datalist);
		resultmap.put("title", titlestr + "售后质量问题饼图" + secret);
		return resultmap;
	}

	/**
	 * 获取批产型号按阶段统计图数据
	 *
	 * @param charttype 1 柱图 2饼图
	 * @auth zzy
	 */
	public Map<String, Object> getProduceChartDataByWorkPhase(int charttype,
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> querymap) {
		querymap.putAll(ProcessingTimeUtil
				.converSimpleDateRange(wQbdReportTemplate.getQualityProblem(),
						wQbdReportTemplate.getQualityProblemStart(),
						wQbdReportTemplate.getQualityProblemEnd()));
		String titleStr = ProcessingTimeUtil
				.converDateToTitleStr(wQbdReportTemplate.getQualityProblem(),
						wQbdReportTemplate.getQualityProblemStart(),
						wQbdReportTemplate.getQualityProblemEnd());
		//指定型号研制阶段
		querymap.put("modeldev", "'批产'");
		querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
		List<Map<String, Object>> list = wQbdFalProblemresetDao
				.getProduceChartDataByWorkPhase(querymap);
		int secretValue = 0;
		String secret = "";
		for (Map dataMap : list) {
			secretValue = getSecretValue(dataMap, secretValue);
			if (0 != secretValue) {
				secret = getSecret(String.valueOf(secretValue));
			}
		}
		if (charttype == CHART_BAR) {
			titleStr = titleStr + "批产型号质量问题按阶段统计柱状图" + secret;
		} else if (charttype == CHART_PIE) {
			titleStr = titleStr + "批产型号质量问题按阶段统计饼图" + secret;
		}
		return commonChartDateByWorkPhase(list, charttype, titleStr);
	}

	/**
	 * 研制型号按阶段统计数据
	 *
	 * @param charttype 1 柱图 2饼图
	 * @auth zzy
	 */
	public Map<String, Object> getModelDevChartDateByWorkPhase(int charttype,
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> querymap) {
		querymap.putAll(ProcessingTimeUtil
				.converSimpleDateRange(wQbdReportTemplate.getQualityProblem(),
						wQbdReportTemplate.getQualityProblemStart(),
						wQbdReportTemplate.getQualityProblemEnd()));
		String titlestr = ProcessingTimeUtil
				.converDateToTitleStr(wQbdReportTemplate.getQualityProblem(),
						wQbdReportTemplate.getQualityProblemStart(),
						wQbdReportTemplate.getQualityProblemEnd());
		Map<String, Object> resultmap = new LinkedHashMap<>();
		//指定型号研制阶段
		querymap.put("modeldev", "'模样', '初样', '试样', '定型'");
		querymap.put("belongdeptname", BaseUtil.getCurrentTenOrgCodePath() + "%");
		List<Map<String, Object>> list = wQbdFalProblemresetDao
				.getModelDevChartDateByWorkPhase(querymap);
		int secretValue = 0;
		String secret = "";
		for (Map dataMap : list) {
			secretValue = getSecretValue(dataMap, secretValue);
			if (0 != secretValue) {
				secret = getSecret(String.valueOf(secretValue));
			}
		}

		if (charttype == CHART_BAR) {
			titlestr = titlestr + "研制型号质量问题按阶段统计柱状图" + secret;
		} else if (charttype == CHART_PIE) {
			titlestr = titlestr + "研制型号质量问题按阶段统计饼图" + secret;
		}
		return commonChartDateByWorkPhase(list, charttype, titlestr);
	}

	/**
	 * 用于处理 研制型号按阶段统计,批产型号按阶段统计 的通用结构
	 *
	 * @auth zzy
	 */
	public Map<String, Object> commonChartDateByWorkPhase(List<Map<String, Object>> list,
			int charttype, String titlestr) {
		Map<String, Object> resultmap = new LinkedHashMap<>();
		if (null == list || list.size() < 1) {
			return resultmap;
		}
		List<String> titlelist = new ArrayList<>();
		if (charttype == CHART_BAR) {
			List<Integer> datalist = new ArrayList<>();
			for (Map<String, Object> stringObjectMap : list) {
				titlelist.add(stringObjectMap.get("name").toString());
				datalist.add(Integer.valueOf(stringObjectMap.get("value").toString()));
			}
			resultmap.put("datalist", datalist);
		} else if (charttype == CHART_PIE) {
			for (Map<String, Object> stringObjectMap : list) {
				titlelist.add(String.valueOf(stringObjectMap.get("name")));
			}
			resultmap.put("datalist", list);
		}
		resultmap.put("title", titlestr);
		resultmap.put("xtitlelist", titlelist);
		return resultmap;
	}

	/**
	 * 获取各院,各所的前五位质量问题
	 *
	 * @auth zzy
	 */
	public Map<String, Object> getTop5(WQbdReportTemplate wQbdReportTemplate,
			Map<String, Object> querymap) {
		querymap.putAll(ProcessingTimeUtil
				.converSimpleDateRange(wQbdReportTemplate.getQualityProblemTopFive(),
						wQbdReportTemplate.getQualityProblemTopFiveStart(),
						wQbdReportTemplate.getQualityProblemTopFiveEnd()));
		Map<String, Object> resultmap = new LinkedHashMap<>();
		String orgid = null;
		try {
			orgid = scriptimpl.getCurrentOrgId();
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<SysOrg> orglist = extendSysOrgService.querySubSysOrgBySupOrgId(orgid);
		//2020/7/8 zzy 手动将当前用户的org放进list...
		orglist.add(scriptimpl.getCurrentOrg());
		List<Map<String, Object>> resultlist = new ArrayList<>();
		for (SysOrg sysOrg : orglist) {
			querymap.put("orgcode", sysOrg.getCode());
			List<Map<String, Object>> list = new ArrayList<>();
			if (BaseUtil.getCurrentSystemLevel() == BaseUtil.SystemLevel.GROUPLEVEL) {
				list = wQbdFalProblemresetDao.getTop5ByFilldepthospitalid(querymap);
			} else {
				list = wQbdFalProblemresetDao.getTop5ByFilldeptiid(querymap);
			}
			resultlist.addAll(list);
		}
		resultmap.put("table2", resultlist);
		return resultmap;
	}


	private int getSecretValue(Map dataMap, int secretValue) {
		if (null != dataMap.get("SECRET")
				&& Integer.parseInt((String) dataMap.get("SECRET")) > secretValue) {
			secretValue = Integer.parseInt((String) dataMap.get("SECRET"));
		}
		return secretValue;
	}


	private String getSecret(String secretValue) {
		DictionaryService dictionaryService = AppUtil.getBean(DictionaryService.class);
		List<Dictionary> dicList = dictionaryService.getDicByNodeKey("mj", false);
		String secret = "";
		for (Dictionary dictionary : dicList) {
			if (dictionary.getItemValue().equals(secretValue)) {
				// 密级显示名称
				secret = dictionary.getItemName();
				break;
			}
		}
		return secret;
	}

	public Map<String, Object> getProblemChartDateByMonth(int chartType,
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> paramMap) {
		paramMap.putAll(getParamMap(wQbdReportTemplate));
		String title = "";
		Map<String, Object> returnMap = Maps.newHashMap();
		List<Map<String, Object>> timeMapList = (List<Map<String, Object>>) paramMap.get("timeMap");
		List legendData = Lists.newArrayList();
		List seriesData = Lists.newArrayList();
		List<Map> pieDataList = Lists.newArrayList();

		for (Map timeMap : timeMapList) {
        paramMap.put("startTime",timeMap.get("startTime"));
        paramMap.put("endTime",timeMap.get("endTime"));
        List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
            .getProblemChartDateByMonth(paramMap);
        for (Map dataMap : dataMapList) {
	        Calendar startCalendar = Calendar.getInstance();
	        startCalendar.setTime((Date) timeMap.get("startTime"));
                seriesData.add(startCalendar.get(Calendar.MONTH)+1+"月");
                legendData.add(Integer.parseInt(String.valueOf(dataMap.get("COUNT"))));
                if (chartType == CHART_PIE) {
                    Map pieMap = Maps.newHashMap();
                    pieMap.put("name", startCalendar.get(Calendar.MONTH)+1+"月");
                    pieMap.put("value", Integer.parseInt(String.valueOf(dataMap.get("COUNT"))));
                    pieDataList.add(pieMap);
                }
            }
        }

		if (chartType == CHART_BAR) {
			returnMap.put("datalist", legendData);
			returnMap.put("title",
					paramMap.get("title").toString() + "年" + title + "型号质量问题按原因柱状图");
		} else {
			returnMap.put("title",
					paramMap.get("title").toString() + "年" + title + "型号质量问题按原因统计");
			returnMap.put("datalist", pieDataList);
		}

		returnMap.put("xtitlelist", seriesData);
		return returnMap;


	}
	public Map<String, Object> getProblemChartDateByDutyRoom(int chartType,
			WQbdReportTemplate wQbdReportTemplate, Map<String, Object> paramMap) {
		paramMap.putAll(getParamMap(wQbdReportTemplate));
//		String title = "";
		Map<String, Object> returnMap = Maps.newHashMap();
		List legendData = Lists.newArrayList();
		List seriesData = Lists.newArrayList();
		List<Map> pieDataList = Lists.newArrayList();
			List<Map<String, Object>> dataMapList = wQbdFalProblemresetDao
					.getProblemChartDateByDutyRoom(paramMap);
			for (Map dataMap : dataMapList) {
				seriesData.add(String.valueOf(dataMap.get("ROOMNAME")));
				legendData.add(Integer.parseInt(String.valueOf(dataMap.get("COUNT"))));
				if (chartType == CHART_PIE) {
					Map pieMap = Maps.newHashMap();
					pieMap.put("name", String.valueOf(dataMap.get("ROOMNAME")));
					pieMap.put("value", Integer.parseInt(String.valueOf(dataMap.get("COUNT"))));
					pieDataList.add(pieMap);
				}
			}

		if (chartType == CHART_BAR) {
			returnMap.put("datalist", legendData);
//			returnMap.put("title",
//					paramMap.get("title").toString() + "年" + title + "型号质量问题按原因柱状图");
		} else {
//			returnMap.put("title",
//					paramMap.get("title").toString() + "年" + title + "型号质量问题按原因统计");
			returnMap.put("datalist", pieDataList);
		}

		returnMap.put("xtitlelist", seriesData);
		return returnMap;


	}
}
